﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NhanVien_frmQuyTacDe : System.Web.UI.Page
{
    DataClassesDataContext qldt = new DataClassesDataContext();
    clsQtDeThi Ob_qtdt = new clsQtDeThi();
    int Kho,TB,De;
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (btnThem.Text=="Luu")
        //{
        //    int soCH = Convert.ToInt32(txtSoluongCH.Text);
        
        if (!Page.IsPostBack)
        {
            
            
            if (qldt.tbl_QUYTACDETHIs.Count()!=0)
            {
                this.grvQTDe.SelectedIndex = 0;
                
                txtSoluongCH.Text = grvQTDe.SelectedRow.Cells[2].Text;
                txtKho.Text = grvQTDe.SelectedRow.Cells[3].Text;
                txtTrungbinh.Text = grvQTDe.SelectedRow.Cells[4].Text;
                txtDe.Text = grvQTDe.SelectedRow.Cells[5].Text;
                Trangthaibd();
            }
            Trangthaibd();

        }
    }

    public void Hamdong()
    {
        
        txtSoluongCH.Enabled = false;
        txtKho.Enabled = false;
        txtTrungbinh.Enabled = false;
        txtDe.Enabled = false;

    }

    public void Trangthaibd()
    {
        btnThem.Text = "Them";
              
        btnThem.Enabled = true;
       
        btnXoa.Enabled = true;

        Hamdong();

    }

    protected void btnThem_Click(object sender, EventArgs e)
    {
        if (btnThem.Text == "Them")
        {
            
            txtSoluongCH.Enabled = true;
            txtSoluongCH.Text = null;
            txtKho.Enabled = true;
            txtKho.Text = null;
            txtDe.Enabled = true;
            txtDe.Text = null;
            txtTrungbinh.Enabled = true;
            txtTrungbinh.Text = null;


            btnThem.Text = "Luu";
            btnXoa.Enabled = false;
            
        }
        else if (btnThem.Text == "Luu")
        {

            
            int soCH = Convert.ToInt32(txtSoluongCH.Text);
            
            int Kho = Convert.ToInt32(txtKho.Text);
            int TB = Convert.ToInt32(txtTrungbinh.Text);
            int De = Convert.ToInt32(txtDe.Text);
            int thoigian = Convert.ToInt32(DropDownList1.SelectedItem.Text);
            thoigian *= 60;
            
            
            Ob_qtdt.ThemmoiQTDT( soCH, Kho, TB, De,thoigian);
            grvQTDe.DataBind();

            
            //rfvnoidung.ControlToValidate = txtNoidungCH.Text;
            // rfvDaA.ControlToValidate = txtA.Text;


           
            btnThem.Text = "Them";
            Trangthaibd();

        }
    }
    //protected void btnSua_Click(object sender, EventArgs e)
    //{
    //    if (btnSua.Text == "Sua")
    //    {
    //        //txtMaCH.Enabled = true;
    //        txtSoluongCH.Enabled = true;


    //        txtKho.Enabled = true;
    //        txtTrungbinh.Enabled = true;
    //        txtDe.Enabled = true;
    //        btnSua.Text = "Luu CH";
    //        btnThem.Enabled = false;
    //        btnXoa.Enabled = false;
    //    }
    //    else if (btnSua.Text == "Luu")
    //    {

    //        string maqt = txtMaQuyTac.Text;
    //        int soCH = Convert.ToInt32(txtSoluongCH.Text);
    //        int Kho = Convert.ToInt32(txtKho.Text);
    //        int TB = Convert.ToInt32(txtTrungbinh.Text);
    //        int De = Convert.ToInt32(txtDe.Text);
    //        Ob_qtdt.SuaQTde(maqt, soCH, Kho, TB, De);
    //        grvQTDe.DataBind();
    //        btnSua.Text = "Sua";
    //        Trangthaibd();

    //    }
    //}
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        try
        {
            string maqt = grvQTDe.SelectedRow.Cells[1].Text;
            Ob_qtdt.XoaQTde(maqt);
            grvQTDe.DataBind();
        }
        catch { }
    }
    protected void grvQTDe_SelectedIndexChanged(object sender, EventArgs e)
    {
        //txtMaQuyTac.Text = grvQTDe.SelectedRow.Cells[1].Text;
        txtSoluongCH.Text = grvQTDe.SelectedRow.Cells[2].Text;
        txtKho.Text = grvQTDe.SelectedRow.Cells[3].Text;
        txtTrungbinh.Text = grvQTDe.SelectedRow.Cells[4].Text;
        txtDe.Text = grvQTDe.SelectedRow.Cells[5].Text;
    }
    protected void btnHuyThaotac_Click(object sender, EventArgs e)
    {
        Trangthaibd();
    }
    public int Kiemtragt(int soluongch, int chdangxuly, int chthu2)
    {
        int kq = soluongch - (chdangxuly + chthu2);
        if (kq < 0)
        {
            chdangxuly = soluongch - chthu2;
            return chdangxuly;
        }

        return kq;

    }
    protected void txtKho_TextChanged(object sender, EventArgs e)
    {
        
        int soCH = Convert.ToInt32(txtSoluongCH.Text);

        if (Convert.ToInt32(txtKho.Text) > soCH || Convert.ToInt32(txtKho.Text) < 0)
        {
            txtKho.Text = soCH.ToString();
            txtTrungbinh.Text = Convert.ToString(0); ;
            txtDe.Text = Convert.ToString(0);
            
        }
        

        if (txtKho.Text.Trim() != string.Empty&&txtTrungbinh.Text.Trim() != string.Empty&&txtDe.Text.Trim() != string.Empty)
        {
            txtDe.Text = (soCH - (Convert.ToInt32(txtKho.Text) + Convert.ToInt32(txtTrungbinh.Text))).ToString();
        }
        
          
        if (txtKho.Text != string.Empty)
        {
            int Kho = Convert.ToInt32(txtKho.Text);
            if (txtTrungbinh.Text != string.Empty && txtDe.Text.Trim() == string.Empty)
            {
                int TB = Convert.ToInt32(txtTrungbinh.Text);

                if ((Kiemtragt(soCH, Kho, TB) + TB) == soCH)
                {
                    txtKho.Text = Kiemtragt(soCH, Kho, TB).ToString();
                    Kho = Convert.ToInt32(txtKho.Text);
                    txtDe.Text = Kiemtragt(soCH, Kho, TB).ToString();
                }
                else
                {
                    txtDe.Text = Kiemtragt(soCH, Kho, TB).ToString();
                }
                


            }
            if (txtTrungbinh.Text == string.Empty && txtDe.Text != string.Empty)
            {
                int De = Convert.ToInt32(txtDe.Text);

                if ((Kiemtragt(soCH, Kho, De) + De) == soCH)
                {
                    txtKho.Text = Kiemtragt(soCH, Kho, De).ToString();
                    Kho = Convert.ToInt32(txtKho.Text);
                    txtTrungbinh.Text = Kiemtragt(soCH, Kho, De).ToString();
                }
                else
                {
                    txtTrungbinh.Text = Kiemtragt(soCH, Kho, De).ToString();
                }
                                
            }
            
        }
        if (txtKho.Text != string.Empty && txtTrungbinh.Text != string.Empty && txtDe.Text != string.Empty)
        {

            Kho = Convert.ToInt32(txtKho.Text);
            TB = Convert.ToInt32(txtTrungbinh.Text);
            De = Convert.ToInt32(txtDe.Text);
        }

        //    string tongcong = (Kho + TB + De).ToString();

        //    string soluongch = txtSoluongCH.Text;

        //}
        btnThem.Attributes.Add("onclick", "return test(" + Kho + "," + TB + "," + De + ");");
        
        
    }
    protected void txtTrungbinh_TextChanged(object sender, EventArgs e)
    {
        int soCH = Convert.ToInt32(txtSoluongCH.Text);

        if (Convert.ToInt32(txtTrungbinh.Text) > soCH || Convert.ToInt32(txtTrungbinh.Text) < 0)
        {
                txtTrungbinh.Text = soCH.ToString();
                txtKho.Text = Convert.ToString(0);
                txtDe.Text = Convert.ToString(0);
        }

        if (txtKho.Text.Trim() != string.Empty && txtTrungbinh.Text.Trim() != string.Empty && txtDe.Text.Trim() != string.Empty)
        {
            txtDe.Text = (soCH - (Convert.ToInt32(txtKho.Text) + Convert.ToInt32(txtTrungbinh.Text))).ToString();
        }
        
            
        
        if (txtTrungbinh.Text != string.Empty)
        {
            int TB = Convert.ToInt32(txtTrungbinh.Text);
            if (txtKho.Text != string.Empty && txtDe.Text.Trim() == string.Empty)
            {
                int Kho = Convert.ToInt32(txtKho.Text);

                if (Kiemtragt(soCH, TB, Kho)+Kho==soCH)
                {
                    txtTrungbinh.Text = Kiemtragt(soCH, TB, Kho).ToString();
                    TB = Convert.ToInt32(txtTrungbinh.Text);
                    txtDe.Text = Kiemtragt(soCH, TB, Kho).ToString();
                    
                }
                else
                {
                    txtDe.Text = Kiemtragt(soCH, TB, Kho).ToString();
                }
                    

            


          }
            if (txtKho.Text == string.Empty && txtDe.Text != string.Empty)
            {
                int De = Convert.ToInt32(txtDe.Text);

                if (Kiemtragt(soCH, TB, De)+De==soCH)
                {
                    txtTrungbinh.Text=Kiemtragt(soCH, TB, De).ToString();
                    TB = Convert.ToInt32(txtTrungbinh.Text);
                    txtKho.Text = Kiemtragt(soCH, TB, De).ToString();

                }
                else
                {
                    txtKho.Text = Kiemtragt(soCH, TB, De).ToString();
                }
                    
           }
            
        }
        if (txtKho.Text != string.Empty && txtTrungbinh.Text != string.Empty && txtDe.Text != string.Empty)
        {

            Kho = Convert.ToInt32(txtKho.Text);
            TB = Convert.ToInt32(txtTrungbinh.Text);
            De = Convert.ToInt32(txtDe.Text);
        }

        //    string tongcong = (Kho + TB + De).ToString();

        //    string soluongch = txtSoluongCH.Text;

        //}
        btnThem.Attributes.Add("onclick", "return test(" + Kho + "," + TB + "," + De + ");");
       
    }
    protected void txtDe_TextChanged(object sender, EventArgs e)
    {
        int soCH = Convert.ToInt32(txtSoluongCH.Text);

        if (Convert.ToInt32(txtDe.Text) > soCH || Convert.ToInt32(txtDe.Text) < 0)
        {
            txtDe.Text = soCH.ToString();
            txtTrungbinh.Text = Convert.ToString(0); ;
            txtKho.Text = Convert.ToString(0);
        }

        if (txtKho.Text.Trim() != string.Empty && txtTrungbinh.Text.Trim() != string.Empty && txtDe.Text.Trim() != string.Empty)
        {
            txtTrungbinh.Text = (soCH - (Convert.ToInt32(txtKho.Text) + Convert.ToInt32(txtDe.Text))).ToString();
        }

              
       
        if (txtDe.Text != string.Empty || Convert.ToInt32(txtDe.Text) < 0)
        {
            int De = Convert.ToInt32(txtDe.Text);
            if (txtKho.Text != string.Empty && txtTrungbinh.Text.Trim() == string.Empty)
            {
                int Kho = Convert.ToInt32(txtKho.Text);
                if (Kiemtragt(soCH, De, Kho) + Kho == soCH)
                {
                    txtDe.Text = Kiemtragt(soCH, De, Kho).ToString();
                    De = Convert.ToInt32(txtDe.Text);
                    txtTrungbinh.Text = Kiemtragt(soCH, De, Kho).ToString();

                }
                else {
                    txtTrungbinh.Text = Kiemtragt(soCH, De, Kho).ToString();
                }
       
          
            }
            if (txtKho.Text == string.Empty && txtTrungbinh.Text != string.Empty)
            {
                int TB = Convert.ToInt32(txtTrungbinh.Text);
                if (Kiemtragt(soCH, De, TB)+TB==soCH)
                {
                    txtDe.Text = Kiemtragt(soCH, De, TB).ToString();
                    De = Convert.ToInt32(txtDe.Text);
                    txtKho.Text = Kiemtragt(soCH, De, TB).ToString();
                    
                }
                else
                {
                    txtKho.Text = Kiemtragt(soCH, De, TB).ToString();

                }
                   
            }
            //if (Convert.ToInt32(txtKho.Text) < 0 || Convert.ToInt32(txtDe.Text) < 0 || Convert.ToInt32(txtTrungbinh.Text) < 0)
            //{
            //    txtKho.Text = string.Empty;
            //    txtDe.Text = string.Empty;
            //    txtTrungbinh.Text = string.Empty;
            //}
        }
        if (txtKho.Text != string.Empty && txtTrungbinh.Text != string.Empty && txtDe.Text != string.Empty)
        {

            Kho = Convert.ToInt32(txtKho.Text);
            TB = Convert.ToInt32(txtTrungbinh.Text);
            De = Convert.ToInt32(txtDe.Text);
        }

        //    string tongcong = (Kho + TB + De).ToString();

        //    string soluongch = txtSoluongCH.Text;

        //}
        btnThem.Attributes.Add("onclick", "return test(" + Kho + "," + TB + "," + De + ");");
        
    }
    //protected void txtMaQuyTac_TextChanged(object sender, EventArgs e)
    //{
    //    string mqt = txtMaQuyTac.Text;
    //    if (Ob_qtdt.KTTrungMa(mqt) == 1)
    //    {
    //        txtMaQuyTac.Text = "Trung ma";
    //        txtMaQuyTac.ForeColor = System.Drawing.Color.Red;
    //    }
    //    else
    //    {
    //        txtMaQuyTac.ForeColor = System.Drawing.Color.Black;
    //    }
    //}
    protected void txtSoluongCH_TextChanged(object sender, EventArgs e)
    {
        if (txtSoluongCH.Text.Trim()==string.Empty)
        {
            txtKho.Enabled = false;
            txtTrungbinh.Enabled = false;
            txtDe.Enabled = false;
            
            
            
            txtKho.Text = null;
          
            txtDe.Text = null;
           
            txtTrungbinh.Text = null;

        }
        else
        {
            txtKho.Enabled = true;
            txtTrungbinh.Enabled = true;
            txtDe.Enabled = true;
            txtKho.Text = string.Empty;
            txtTrungbinh.Text = string.Empty;
            txtDe.Text = string.Empty;
        }
    }
}
